package cn.aib.vod.controller;

import cn.aib.commonutils.R;
import cn.aib.servicebase.exception.GuliException;
import cn.aib.vod.service.VodService;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     *  上传视频到阿里云
     *  方法名：uploadVideoToAliYun
     *  参数：MultipartFile file
     *  返回值：R
     */
    @PostMapping("uploadVideo")
    public R uploadVideoToAliYun(MultipartFile file){
        //上传视频，返回视频ID
        String videoId = vodService.uploadVideoToAliYun(file);

        //返回R
        return R.ok().data("videoId",videoId);
    }

    /**
     *  根据视频ID删除阿里云中的视频
     *  方法名：deleteAlyVideo
     *  参数：String id
     *  返回值：R
     *  请求： post
     */
    @DeleteMapping("deleteAlyVideo/{id}")
    public R deleteAlyVideo(@PathVariable("id") String id){
        //上传视频，返回视频ID
        try {
            vodService.deleteAlyVideo(id);
            log.info("删除视频成功");
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(Integer.parseInt(e.getErrCode()), e.getMessage());
        }
        //返回R
        return R.ok();
    }

    /**
     *  根据多个视频ID删除阿里云中的视频
     *  方法名：deleteBatchAlyVideo
     *  参数：List<String> ids
     *  返回值：R
     *  请求： delete
     */
    @DeleteMapping("deleteBatchAlyVideo")
    public R deleteBatchAlyVideo(@RequestParam("ids") List<String> ids){
        //上传视频，返回视频ID
        try {
            vodService.deleteBatchAlyVideo(ids);

            log.info("删除多个视频成功");
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(Integer.parseInt(e.getErrCode()), e.getMessage());
        }
        //返回R
        return R.ok();
    }
    /**
     *  获取视频凭证
     *  方法名：getPlayAuth
     *  参数：String id（视频id）
     *  返回值：R
     */
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        String playAuth = null;
        try {
            playAuth = vodService.getPlayAuthByVid(id);
        } catch (ClientException e) {
            e.printStackTrace();
            return R.error();
        }
        return R.ok().data("playAuth",playAuth);
    }
}
